package org.apache.servicecomb.samples.practise.houserush.realestate.api;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.samples.practise.houserush.realestate.entity.Building;
import org.apache.servicecomb.samples.practise.houserush.realestate.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.ws.rs.Path;
import java.util.List;

@RestSchema(schemaId =  "buildingApi")
@RequestMapping("/")
public class BuildingApiImpl implements BuildingApi {

    @Autowired
    private BuildingService buildingService;

    @Override
    @PostMapping("realestates/{realestateId}/buildings")
    public Building createBuilding(@PathVariable("realestateId") int realestateId,@RequestBody Building building) {

        return buildingService.createBuilding(realestateId,building);
    }

    @Override
    @GetMapping("buildings/{id}")
    public Building findBuilding(@PathVariable("id") int id) {
        return buildingService.findBuilding(id);
    }

    @Override
    @PutMapping("buildings/{id}")
    public Building updateBuilding(@PathVariable("id") int id,@RequestBody Building building) {
        building.setId(id);
        return buildingService.updateBuilding(building);
    }

    @Override
    @DeleteMapping("buildings/{id}")
    public void removeBuilding(@PathVariable("id") int id) {
        buildingService.removeBuilding(id);
    }

    @Override
    @GetMapping("realestates/{realestateId}/buildings")
    public List<Building> indexBuildings(@PathVariable("realestateId") int realestateId) {
        return buildingService.indexBuildings(realestateId);
    }
}
